package com.nsoft.thickclient.trackMove.account;

import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JPanel;

import com.nsoft.server.serviceImpl.FacadeFactoryImpl;
import com.nsoft.thickclient.components.VoPropertyChooserField;
import com.nsoft.thickclient.components.VoPropertySearchField;
import com.nsoft.thickclient.components.VoPropertySearchFieldAction;
import com.nsoft.thickclient.components.VoPropertyTextArea;
import com.nsoft.thickclient.containers.NBoxPanel;
import com.nsoft.thickclient.containers.VOPropertyComponentPanel;
import com.nsoft.thickclient.controller.ScreenView;
import com.nsoft.thickclient.model.VoModel;
import com.nsoft.thickclient.trackMove.intr.account.Account;
import com.nsoft.thickclient.trackMove.intr.account.AccountType;
import com.nsoft.thickclient.trackMove.intr.party.ThinParty;

public class AccountView extends ScreenView<Account> {

	/**
	 * serialVersionUID = 1L.
	 */
	private static final long serialVersionUID = 1L;

	private VoModel model;

	private final static AccountType[] types = {
			new AccountType(1l, "Capital Accounts"),
			new AccountType(2l, "Fixed Assets"),
			new AccountType(3l, "Cash and Bank"),
			new AccountType(4l, "Debtor and Creditors"),
			new AccountType(5l, "Other Assets"),
			new AccountType(6l, "Other Liabilities"),
			new AccountType(7l, "Services Charges and Billing Acruals"),
			new AccountType(8l, "Expenses") };

	public AccountView() {
		this(new Account(), false);
	}

	public AccountView(boolean isEditMode) {
		this(new Account(), isEditMode);
	}

	public AccountView(Account account, boolean isEditMode) {
		model = new VoModel(account);
		NBoxPanel mainPanel = new NBoxPanel(NBoxPanel.VERTICAL);
		mainPanel.addComponent(getDetailsPanel(isEditMode));
		mainPanel.addComponent(getBalancePane());
		mainPanel.addComponent(getRemarksPanel());
		add(mainPanel);
	}

	private JComponent getDetailsPanel(boolean isEditMode) {
		VOPropertyComponentPanel leftPanel = new VOPropertyComponentPanel();
		leftPanel.addVoTextField(model, "name", "Name", !isEditMode);

		List<AccountType> list = new ArrayList<AccountType>();
		for (AccountType type : types) {
			list.add(type);
		}
		leftPanel.addComponent("Account type", new VoPropertyChooserField(
				model, "type", "id", "name", list));
		leftPanel.addVoDateChooser(model, "creationDate", "Created on",
				!isEditMode);
		leftPanel.addVoDateChooser(model, "updationDate", "Updated on",
				!isEditMode);

		VOPropertyComponentPanel rightPanel = new VOPropertyComponentPanel();
		rightPanel.addVoTextField(model, "number", "Account number",
				!isEditMode);
		//List<ThinParty> list2 = new ArrayList<ThinParty>();
		VoPropertySearchField partySearchField = new VoPropertySearchField(model,"partyId","id",new VoPropertySearchFieldAction() {

			public List<?> getData(String filter) {
				return FacadeFactoryImpl.getPartyService().getAllThinParties(new ThinParty(filter,null));
			}

			public List<?> getDataByMappedavalue(Object value) {
				// TODO Auto-generated method stub
				return null;
			}

		});
		/*rightPanel.addComponent("Party name", new VoPropertyChooserField(model,
				"partyId", "id", "name", list2));*/
		rightPanel.addComponent("Party name",partySearchField);
		rightPanel.addVoDateChooser(model, "openDate", "Opening date",
				!isEditMode);
		rightPanel
				.addVoCheckBox(model, "selectable", "Selectable", !isEditMode);
		JPanel panel = new JPanel(new GridLayout(1, 2));
		panel.setBorder(BorderFactory.createTitledBorder("Account details"));
		panel.add(leftPanel);
		panel.add(rightPanel);
		return panel;
	}

	private JComponent getBalancePane() {
		VOPropertyComponentPanel leftPanel = new VOPropertyComponentPanel();
		leftPanel.addVoNumericField(model, "openingBalance", "Opening balance",
				true, true);
		leftPanel.addVoNumericField(model, "limits", "Limits", true, true);
		VOPropertyComponentPanel rightPanel = new VOPropertyComponentPanel();
		rightPanel.addVoNumericField(model, "currentBalance",
				"Current balance", true, true);
		rightPanel.addStrut(1);
		JPanel panel = new JPanel(new GridLayout(1, 2));
		panel.setBorder(BorderFactory.createTitledBorder("Balance details"));
		panel.add(leftPanel);
		panel.add(rightPanel);
		return panel;
	}

	private JComponent getRemarksPanel() {
		VOPropertyComponentPanel panel = new VOPropertyComponentPanel();
		panel.setBorder(BorderFactory.createTitledBorder("Remarks"));
		VoPropertyTextArea textArea = new VoPropertyTextArea(model, "remarks");
		textArea.setRows(5);
		textArea.setColumns(60);
		panel.addComponent(null, textArea);
		return panel;
	}

	public VoModel getModel() {
		return model;
	}

	public void setModel(VoModel model) {
		this.model = model;
	}

	public Account getWorkingBean() {
		return (Account) model.getValueObject();
	}

	public void setWorkingBean(Account bean) {
		model.setValueObject(bean);

	}

}
